; xy_18.ncl
;
; Concepts illustrated:
;   - Filling the area between two curves in an XY plot
;   - Labeling the bottom X axis with years
;   - Drawing a main title on three separate lines
;   - Calculating a weighted average
;   - Changing the size/shape of an XY plot using viewport resources
;   - Manually creating a legend
;   - Overlaying XY plots on each other
;   - Maximizing plots after they've been created
;
;*****************************************************

time = ispan(1980,2021,1)

indir = "DATA/FIG_4/"

ids = (/"2856900"/)

styr1 = 1980
edyr1 = 2020

xtime1 = yyyymmdd_time(styr1,edyr1,"integer")
xtime11 = yyyymmdd_to_yyyyfrac(xtime1, 0.)

styr2 = 2006
edyr2 = 2008

xtime2 = yyyymmdd_time(styr2,edyr2,"integer")
xtime22 = yyyymmdd_to_yyyyfrac(xtime2, 0.)

styr3 = 2013
edyr3 = 2015

xtime3 = yyyymmdd_time(styr3,edyr3,"integer")
xtime33 = yyyymmdd_to_yyyyfrac(xtime3, 0.)

infile1 = new((/5/),"string")
infile2 = new((/5/),"string")
infile3 = new((/5/),"string")

do i = 0, 4
infile1(i) = indir+"ERA5_PPT_TC_NOTC_"+ids(i)+".csv"
infile2(i) = indir+"VIC_SMroot_TC_NOTC_"+ids(i)+".csv"
infile3(i) = indir+"DISCHARGE_TC_NOTC_"+ids(i)+".csv"
end do

data1 = new((/14976,3,5/),"float")
data2 = new((/14976,3,5/),"float")

do i = 0, 4
dum1 = asciiread(infile1(i),-1,"string")
data1(:,0,i) = stringtofloat(str_get_field(dum1,4,","))
data2(:,0,i) = stringtofloat(str_get_field(dum1,5,","))
delete(dum1)
dum1 = asciiread(infile2(i),-1,"string")
data1(:,1,i) = stringtofloat(str_get_field(dum1,4,","))
data2(:,1,i) = stringtofloat(str_get_field(dum1,5,","))
delete(dum1)
dum1 = asciiread(infile3(i),-1,"string")
data1(:,2,i) = stringtofloat(str_get_field(dum1,4,","))
data2(:,2,i) = stringtofloat(str_get_field(dum1,5,","))
delete(dum1)
end do

;***************************************************** 
; create plot
;***************************************************** 

pltDir = "FIG_MAIN/" ; where output will be written
pltName= "Fig_4b"
pltType= (/"eps","png","x11"/)

plot = new(6, graphic)

do m = 0, 2

  pltPath := pltDir+pltName
  wks     := gsn_open_wks(pltType(m), pltPath)

  res                    = True              ; plot mods desired
  
  res@gsnDraw            = False             ; don't draw yet
  res@gsnFrame           = False             ; don't advance frame yet
  res@gsnMaximize        = False    ; JK: Customize the size of sub plots.

  res@vpHeightF 	 = 0.12               ; change aspect ratio of plot
  res@vpWidthF 	         = 0.8
  res@vpXF = 0.11
  res@vpYF = 0.975             ; default is 0.8  

  res@trYMinF            = min(data2(9498:10227,0,3))-0.05*min(data2(9498:10227,0,3))
  res@trYMaxF            = max(data1(9498:10227,0,3))+0.05*max(data1(9498:10227,0,3))
  res@trXMinF	         = 2006 ;1980              ; set x-axis minimum
  res@trXMaxF	         = 2008 ;2020              ; set x-axis maximum

;Tick Mark
  res@tmXTOn = "False"
  res@tmXBOn = True
  res@tmXBMode        = "Explicit"
  res@tmXBValues      = (/2006,2007,2008/)
  res@tmXBLabelsOn = "False" 
  res@tmXBLabelDeltaF = -0.6
  res@tmXBLabelFontHeightF = 0.012  
  res@tmXBMajorOutwardLengthF = 0.005 
  res@tmYLLabelsOn = True

  res@gsnLeftStringFontHeightF	 = 0.012
  res@gsnLeftString   = "B) GODAVARI River (GRDC ID:"+ids(0)+")"
 
  res@xyMonoLineColor    = False             ; want colored lines
  res@xyLineColors       = (/"royalblue4","Red"/) ; colors chosen
  res@xyLineThicknesses	 = (/3.,3./)      ; line thicknesses
  res@xyDashPatterns	 = (/0.,0./)      ; make all lines solid
  res@tiYAxisOn = "False"
  res@tmYLLabelFontHeightF = 0.011
  res@tmXMajorGrid = "True"
  res@tmXMajorGridLineDashPattern = 1
  
  plot(0) = gsn_csm_xy (wks,xtime11,(/data1(:,0,3),data2(:,0,3)/),res)       ; create line plot

  res@trYMinF            = min(data2(9498:10227,1,3))-0.05*min(data2(9498:10227,1,3))
  res@trYMaxF            = max(data1(9498:10227,1,3))+0.05*max(data1(9498:10227,1,3))
  res@gsnLeftString   = ""
  res@vpYF = 0.8555              ; default is 0.8
  res@tmYLLabelsOn = False
  res@tmYRLabelsOn = True

  plot(1) = gsn_csm_xy (wks,xtime11,(/data1(:,1,3),data2(:,1,3)/),res)

  res@trYMinF            = min(data2(9498:10227,2,3))-0.05*min(data2(9498:10227,2,3))
  res@trYMaxF            = max(data1(9498:10227,2,3))+0.05*max(data1(9498:10227,2,3))
  res@gsnLeftString   = ""
  res@vpYF = 0.735              ; default is 0.8
  res@tmYLLabelsOn = True
  res@tmYRLabelsOn = False
  res@tmXBOn = True
  res@tmXBMode        = "Explicit"
  res@tmXBLabelsOn = True
  res@tmXBValues      = (/2006,2007,2008/)
  res@tmXBLabels      = (/2006,2007,2008/)
  res@tmXBLabelDeltaF = -0.6
  res@tmXBLabelFontHeightF = 0.012

  plot(2) = gsn_csm_xy (wks,xtime11,(/data1(:,2,3),data2(:,2,3)/),res)

;*****************************************************   
; Manually create and attach legend
;***************************************************** 
  res_text                    = True                  ; text mods desired
  res_text@txFontHeightF      = 0.009                 ; change text size
  res_text@txJust             = "CenterLeft"          ; text justification

  res_lines                   = True                  ; polyline mods desired
  res_lines@gsLineDashPattern = 0.                    ; solid line
  res_lines@gsLineThicknessF  = 5.                    ; line thicker
  res_lines@gsLineColor       = "seagreen"                 ; line color
  xx = (/1980.5,1981.5/)
  yy = (/0.90,0.90/)
;  dum1 = gsn_add_polyline(wks,plot(0),xx,yy,res_lines)              ; add polyline
;  dum2 = gsn_add_text(wks,plot(0),"Total",1981.75,0.90,res_text); add text
  
   yy = (/0.75,0.75/)
  res_lines@gsLineColor       = "royalblue4"                                 ; change to blue
;  dum3 = gsn_add_polyline(wks,plot(0),xx,yy,res_lines)                ; add polyline
;  dum4 = gsn_add_text(wks,plot(0),"Short Duration",1981.75,0.75,res_text)       ; add text
  
   yy = (/0.60,0.60/)
  res_lines@gsLineColor       = "red"                                ; change to black
;  dum5 = gsn_add_polyline(wks,plot(0),xx,yy,res_lines)                ; add polyline
;  dum6 = gsn_add_text(wks,plot(0),"Long Duration",1981.75,0.60,res_text) ; add text

;JK------Reference line at y=0----

x11=(/0,3000/)
y11=(/0,0/)

  res_lines@gsLineDashPattern = 0.                    ; solid line
  res_lines@gsLineThicknessF  = 1.
  res_lines@gsLineColor       = "black"

  dum7 = gsn_add_polyline(wks,plot(0),x11,y11,res_lines) 
  dum8 = gsn_add_polyline(wks,plot(1),x11,y11,res_lines)
  dum9 = gsn_add_polyline(wks,plot(2),x11,y11,res_lines)
  dum10 = gsn_add_polyline(wks,plot(3),x11,y11,res_lines)
  dum11 = gsn_add_polyline(wks,plot(4),x11,y11,res_lines)
  dum12 = gsn_add_polyline(wks,plot(5),x11,y11,res_lines)

;JK-------YAxis Title-------------
  txres1 = True
  txres1@txJust = "CenterRight"
  txres1@txFontColor = "black"
  txres1@txAngleF = 90.0
  txres1@txFontHeightF = 0.011
  txres1@txFontThicknessF = 4.0
  gsn_text_ndc(wks, "PREC. [mm/day]", 0.043, 0.963, txres1)
  gsn_text_ndc(wks, "Discharge [m~S~3~N~/s]", 0.03, 0.725, txres1)
  txres1@txJust = "CenterLeft"
  txres1@txAngleF = 270.0
  gsn_text_ndc(wks, "SM [mm]", 0.975, 0.82, txres1)


;JK------------

  pgres = True
  pgres@gsEdgesOn = False
  pgres@gsFillColor = "gray"
  pgres@gsFillOpacityF = 0.2
;  ddum1 = gsn_add_polygon(wks,plot(2),(/1985,1987,1987,1985/),(/0, 0, 200, 200/),pgres)
;  ddum2 = gsn_add_polygon(wks,plot(3),(/2006,2008,2008,2006/),(/0, 0, 200, 200/),pgres)
;  ddum3 = gsn_add_polygon(wks,plot(4),(/2013,2015,2015,2013/),(/0, 0, 200, 200/),pgres)

  regs = (/"a", "b", "c"/)
  
  txres = True
  txres@txJust = "CenterLeft"
  txres@txFontColor = "black"
  txres@txAngleF = 0.0
  txres@txFontThicknessF = 4.0
  txres@txFontHeightF = 0.0135

;  tdum1 = gsn_add_text(wks,plot(2),regs(0),1985.2,10.0,txres)
;  tdum2 = gsn_add_text(wks,plot(3),regs(1),2006.2,2.2,txres)
;  tdum3 = gsn_add_text(wks,plot(4),regs(2),2013.2,2.8,txres)

 draw(plot)
 frame(wks)

end do
